<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">权限管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <br />
    <el-card>
      <el-button type="primary" @click="addjuese">添加角色</el-button> <br />
      <br />
      <template>
        <el-table :data="list" border style="width: 100%">
          <el-table-column label="" type="expand">
            <template slot-scope="scope">
              <div class="role_box">
                <div v-if="scope.row.children.length > 0"> 
                  <el-row
                    v-for="item1 in scope.row.children"
                    :key="item1.id"
                    class="row_box1"
                  >
                    <!-- 第一列 -->
                    <el-col :span="4">
                      <el-tag
                        type="primary"
                        closable
                        @close="deleteRights(scope.row,scope.row.id, item1.id)"
                        >{{ item1.authName }}</el-tag
                      >
                    </el-col>
                    <!-- 第二列 -->
                    <el-col :span="20">
                      <el-row v-for="item2 in item1.children" :key="item2.id">
                        <el-col :span="6">
                          <el-tag
                            type="danger"
                            closable
                            @close="deleteRights(scope.row,scope.row.id, item2.id)"
                            >{{ item2.authName }}</el-tag
                          >
                        </el-col>
                        <el-col :span="18">
                          <el-tag
                            type="warning"
                            closable
                            @close="deleteRights(scope.row,scope.row.id, item3.id)"
                            v-for="item3 in item2.children"
                            :key="item3.id"
                            >{{ item3.authName }}</el-tag
                          >
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </div>
                <div v-else>没有权限</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="#" type="index"> </el-table-column>
          <el-table-column prop="roleName" label="角色名称"></el-table-column>
          <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="bianji(scope.row)"
                >编辑</el-button
              >
              <el-button type="danger" size="mini" @click="del(scope.row.id)"
                >删除</el-button
              >
              <el-button
                type="warning"
                size="mini"
                @click="setRoleTo(scope.row)"
                >设置</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <!-- 添加角色 -->
    <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
      <el-form :model="user" :rules="rules" ref="ruleForm">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="roleName"
        >
          <el-input v-model="user.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="formLabelWidth"
          prop="roleDesc"
        >
          <el-input v-model="user.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改角色 -->
    <el-dialog title="修改角色" :visible.sync="dialogFormVisibles">
      <el-form :model="addusers" :rules="rules" ref="xgaddForms">
        <el-form-item
          label="角色名称"
          prop="roleName"
          :label-width="formLabelWidth"
        >
          <el-input v-model="addusers.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          prop="roleDesc"
          :label-width="formLabelWidth"
        >
          <el-input v-model="addusers.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibles = false">取 消</el-button>
        <el-button type="primary" @click="tjbianji">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分配权限的对话框 -->

    <el-dialog
      title="提示"
      :visible.sync="dialogVisiblesss"
      width="30%"
      :before-close="handleClose"
    >
      <el-tree
        ref="treelist"
        :data="treeData"
        show-checkbox
        node-key="id"
        :default-checked-keys="checkall"
        default-expand-all
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblesss = false">取 消</el-button>
        <el-button type="primary" @click="addsetRoleTo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getjueseApi,
  addjueseApi,
  getzhuangApi,
  deljueseApi,
  bianjueseApi,
  getquanxianApi,
  getrolelist,
  shanjueseApi,
} from "@/http/api";
export default {
  data() {
    return {
      defaultProps: {
        label: "authName",
        children: "children",
      }, //tree默认配置项
      treeData: [],
      user: {
        //角色列表
        roleName: "",
        roleDesc: "",
      },
      addusers: {},
      adduser: {
        //修改角色列表
        roleName: "",
        roleDesc: "",
        id: 0,
      },
      list: [],
      dialogVisiblesss: false, //分配权限的弹框
      dialogFormVisible: false, //添加的框
      dialogFormVisibles: false, //修改的框
      formLabelWidth: "90px",
      checkall: [],
      roleid: "", //角色的id
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    //删除角色指定权限
    async deleteRights(row,roleid, rightid) {
      console.log(roleid, rightid);
      let res = await shanjueseApi(roleid, rightid);
      console.log("res", res);
      row.children = res;
      // this.getlist();
    },
    //监听关闭对话框
    handleClose() {},
    //打开分配权限对话框
    async setRoleTo(row) {
      console.log("row", row);
      this.dialogVisiblesss = true;
      let res = await this.$API.getquanxianApi("tree");
      this.treeData = res;
      console.log(this.treeData);
      this.roleid = row.id;
      this.getchecked(row,this.checkall) // row当前点击的数据  
      console.log("当前点击的",row)
      console.log("角色拥有的权限",this.checkall) 
    },
     getchecked(row,checkall){
         if(!row.children){
             return  checkall.push(row.id)
         }
         row.children.forEach(item=>{
             this.getchecked(item,checkall) 
         })
    },
    //点击确定时关闭模态框
    async addsetRoleTo() {
      this.dialogVisiblesss = false;
      const allcheck = this.$refs.treelist.getCheckedKeys(); //全选节点
      const bancheck = this.$refs.treelist.getHalfCheckedKeys(); //半选节点
      const hebing = [...allcheck, ...bancheck]; //进行合并
      const arr = hebing.join(","); //以逗号分割
      let res = await getrolelist(this.roleid, arr);
      this.getlist();
    },
    async getlist() {
      let res = await getjueseApi();
      this.list = res;
      console.log("this.list", this.list[1].id);

      // console.log(res)
    },
    //点击添加角色按钮
    addjuese() {
      this.dialogFormVisible = true;
    },
    //点击确定按钮添加
    async add() {
      console.log("添加角色啦");
      let res = await addjueseApi(this.user); //添加定义的列表
      console.log("res", res);
      this.dialogFormVisible = false;
      this.getlist();
    },
    del(id) {
      console.log("删除的id", id);
      this.$confirm("确定删除这条信息嘛", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await deljueseApi(id);
          this.getlist();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑数据回填
    bianji(row) {
      console.log(row);
      this.dialogFormVisibles = true;
      this.addusers = row;
    }, 
    //提交编辑
    async tjbianji(){ 
      console.log(this.addusers);
      let id = this.addusers.id;
      let roleName = this.addusers.roleName; 
      let roleDesc=this.addusers.roleDesc 
      console.log(this.roleDesc)  
      let res = await bianjueseApi(id, this.addusers);
      this.getlist();
      this.dialogFormVisibles = false;
    },
  },
};
</script>

<style scoped>
.role_box {
  padding: 20px;
}
.row_box1 {
  margin: 10px;
}
.el-tag {
  margin: 13px;
}
</style>
